package com.reggie.config;

import com.reggie.interceptor.JwtTokenAdminInterceptor;
import com.reggie.interceptor.JwtTokenUserInterceptor;
import com.reggie.json.JacksonObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.List;

@Configuration
@Slf4j
public class WebMvcConfig implements WebMvcConfigurer {
    @Autowired
    private JwtTokenAdminInterceptor jwtTokenAdminInterceptor;
    @Autowired
    private JwtTokenUserInterceptor jwtTokenUserInterceptor;


    /**
     * 注册拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        log.info("开始注册自定义拦截器...");
        registry.addInterceptor(jwtTokenAdminInterceptor)
                .addPathPatterns("/admin/**")
                .excludePathPatterns("/admin/employee/login","/admino/employee/logout");
        //放行user
        registry.addInterceptor(jwtTokenUserInterceptor)
                .addPathPatterns("/user/**")
                .excludePathPatterns("/user/user/login","/user/user/logout");
        WebMvcConfigurer.super.addInterceptors(registry);
    }

    /**
     * 生成商家端接口文档的设置
     * @return
     */
    @Bean
    public Docket docket1(){
        log.info("准备生成接口文档");

        ApiInfo build = new ApiInfoBuilder()
                .title("瑞吉外卖项目接口文档")
                .version("2.0")
                .description("瑞吉外卖项目接口文档")
                .build();

        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .groupName("管理员端接口")
                .apiInfo(build)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.reggie.controller.admin"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }
    /**
     * 生成用户端接口文档的设置
     * @return
     */
    @Bean
    public Docket docket2(){
        log.info("准备生成接口文档");

        ApiInfo build = new ApiInfoBuilder()
                .title("瑞吉外卖项目接口文档")
                .version("2.0")
                .description("瑞吉外卖项目接口文档")
                .build();

        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .groupName("用户端接口")
                .apiInfo(build)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.reggie.controller.user"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

    /**
     * 设置静态资源映射
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.info("开启静态资源的映射。。。");
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        WebMvcConfigurer.super.addResourceHandlers(registry);
    }

    /**
     * 扩展 spring MVC消息转换器
     * @param converters
     */
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        log.info("开始扩展消息转换器........");
        //创建一个消息装换器对象
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        //设置消息装换器格式
        converter.setObjectMapper(new JacksonObjectMapper());
        //将消息装换器转添加到springmvc中自带的装换器中
        converters.add(0,converter);
    }
}
